﻿using System;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.UI;


[ExecuteInEditMode]
[RequireComponent(typeof(Text))]
public class SetColor : MonoBehaviour
{
    public string TextColorName = "White";
    public ColorAssets assets
    {
        get
        {
            return _sColorAsset;
        }
    }
    static ColorAssets _sColorAsset;

    public static Color GetColor(string colorName)
    {
        if(_sColorAsset == null)
        {
#if UNITY_EDITOR
            _sColorAsset = AssetDatabase.LoadAssetAtPath<ColorAssets>("Assets/Dungeon/Res/Internal/Color/ColorAssets.asset");
#else
            _sColorAsset = AppFacade.resourceManager.LoadAsset("Res/Internal/Color/ColorAssets.asset") as ColorAssets;
#endif
        }
        return _sColorAsset[colorName].changeItem;
    }

    private Text _textComponent;

    private void Awake()
    {
        _textComponent = GetComponent<Text>();
        OnChange();
    }

#if UNITY_EDITOR
    private void Update()
    {
        if (EditorApplication.isPlaying)
            return;

        OnChange();
    }
#endif

    void OnChange()
    {
        if (assets == null)
        {
#if UNITY_EDITOR
            _sColorAsset = AssetDatabase.LoadAssetAtPath<ColorAssets>("Assets/Dungeon/Res/Internal/Color/ColorAssets.asset");
#else
            _sColorAsset = AppFacade.resourceManager.LoadAsset("Res/Internal/Color/ColorAssets.asset") as ColorAssets;
#endif
            return;
        }

        if(TextColorName != null && TextColorName != ""){
        _textComponent.color = GetColor(TextColorName);
        }
    }
}

